package myDaos;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.entities.UnmappedItem;


@Stateless
public class UnmappedItemsDao {

	
	
	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	
	public List<UnmappedItem> getUnmappedItems(String country,String distributor)
	{
		List<UnmappedItem> umlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT d FROM UnmappedItem d WHERE 1=1 "); 
		    	
			if(country!=null && !country.trim().equals(""))
    	    {
				queryBiffer.append(" and d.country = :country") ;	
    	    }
			
			if(distributor!=null && !distributor.trim().equals(""))
    	    {
				queryBiffer.append(" AND d.distributorNo IN (:distributor)") ;	
    	    }
			
			Query q = getEntityManager().createQuery(queryBiffer.toString());
			if(country!=null && !country.trim().equals("")){q.setParameter("country", country);} 
			if(distributor!=null && !distributor.trim().equals("")){q.setParameter("distributor", distributor);}
			    umlist = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return umlist;
		  
	}
	
	
}
